<?php

namespace Admin\Controller;

use Think\Controller;

class DoctorController extends BaseController {
    /*
     * 医生列表
     */

    public function doctor_list() {
        //表单搜索条件
        $name = I('name');
        $section = I('section');
        $title = I('title');

        if (!empty($name)) {
            $map['a.name'] = array('like', '%' . $name . '%');
        }
        if (!empty($section)) {

            $map['a.tid'] = M("section_type")->where("name='" . $section . "'")->getField("id");
        }
        if (!empty($title)) {
            $map['a.title'] = $title;
        }
        //保证查询时，分页页码从1开始
        if (IS_POST) {
            $_GET['p'] = 1;
        }
        $count = M('doctor')->alias("a")->where($map)->count();
        $pagesize = 15;
        $Page = new \Think\Page($count, $pagesize);

        if (!empty($map)) {
            $param = empty($param) ? I('') : array_merge($param, I(''));
            //分页跳转的时候保证查询条件
            if (!empty($param)) {
                foreach ($param as $key => $val) {
                    $Page->parameter[$key] = urlencode($val);
                }
            }
        }
        $res = M('doctor')
                ->field("a.*,t.name as tname")
                ->alias("a")
                ->join("left join section_type as t on t.id=a.tid")
                ->where($map)
                ->order('a.did desc')
                ->limit($Page->firstRow, $Page->listRows)
                ->select();
        $this->assign("data", $res);
        $this->assign("page", $Page->show());
        $this->display();
    }

    /**
     * 添加医生
     */
    public function doctor_add() {
        if (IS_POST) {
            $name = I("name"); //名字
            $tid = I("tid");
            $phone = I("phone"); //电话
            $title = I("title"); //职称
            $intro = I("intro"); //简介
            $lng = I("lng"); //经度
            $lat = I("lat"); //纬度
            $address = I("address"); //详细地址
            $reception_sun = I("reception_sun"); //每天接诊量
            $working = I("working"); //上班时间
            $getoff = I("getoff"); //下班时间
            if (empty($name) || empty($phone) || empty($title) || empty($intro) || empty($lat) || empty($lng) || empty($address)) {
                $this->error("参数不完整");
                exit;
            } else {
                $res = upload_img();
                if (!$res) {
                    $this->error("上传头像失败");
                    exit;
                } else {
                    $data["header"] = IMG_SERVER . "/" . $res["header"]["savepath"] . $res["header"]["savename"];
                    $data["tid"] = $tid;
                    $data["name"] = $name;
                    $data["phone"] = $phone;
                    $data["title"] = $title;
                    $data["intro"] = $intro;
                    $data["lng"] = $lng;
                    $data["lat"] = $lat;
                    $data["address"] = $address;
                    $data["reception_sun"] = $reception_sun;
                    $data["working"] = $working;
                    $data["getoff"] = $getoff;
                    $data["created"] = time();
                    $re = M("doctor")->add($data);
                    if ($re) {
                        $this->success("添加成功");
                        exit;
                    } else {
                        $this->error("添加失败");
                        exit;
                    }
                }
            }
        }
        $res = M("section_type")->select();
        $this->assign("section", $res);
        $this->display();
    }

    /**
     * 医生编辑
     */
    public function doctor_edit() {
        if (IS_POST) {
            $name = I("name"); //名字
            $tid = I("tid");
            $phone = I("phone"); //电话
            $title = I("title"); //职称
            $intro = I("intro"); //简介
            $lng = I("lng"); //经度
            $lat = I("lat"); //纬度
            $address = I("address"); //详细地址
            $reception_sun = I("reception_sun"); //每天接诊量
            $working = I("working"); //上班时间
            $getoff = I("getoff"); //下班时间
            if (empty($name) || empty($phone) || empty($title) || empty($intro) || empty($lat) || empty($lng) || empty($address)) {
                $this->error("参数不完整");
                exit;
            } else {
                $res = upload_img();
                if (!$res) {
                   $data["header"] = I("header");
                } else {
                    $data["header"] = IMG_SERVER . "/" . $res["new_header"]["savepath"] . $res["header"]["savename"];
                }
                
                $data["tid"] = $tid;
                $data["name"] = $name;
                $data["phone"] = $phone;
                $data["title"] = $title;
                $data["intro"] = $intro;
                $data["lng"] = $lng;
                $data["lat"] = $lat;
                $data["address"] = $address;
                $data["reception_sun"] = $reception_sun;
                $data["working"] = $working;
                $data["getoff"] = $getoff;
                $where["did"] = I("did");
                $re = M("doctor")->where($where)->save($data);
                if($re){
                    $this->success("修改成功",U("doctor/doctor_list"));exit;
                }else{
                    $this->error("内容未做修改");exit;
                }
            }
        }
        $id = I("did");
        $re = M("doctor")->where("did=" . $id)->find();
        $res = M("section_type")->select();
        $this->assign("section", $res);
        $this->assign("data", $re);
        $this->display();
    }

    /**
     * 医生删除
     */
    public function doctor_del() {
        $id = I("did");
        $re = M("doctor")->where("did=" . $id)->delete();
        if ($re) {
            $this->success("删除成功", U("doctor/doctor_list"));
        } else {
            $this->error("删除失败");
        }
    }

}
